// 笔记存储工具
const NOTE_KEY = 'notes_data'
const NOTE_CONFIG = 'notes_config'
const UNCATEGORIZED = '未分类'

function setConfig(config) {
  wx.setStorageSync(NOTE_CONFIG, config)
}

function getConfig() {
  return wx.getStorageSync(NOTE_CONFIG)
}

// 初始化数据
function initData() {
  const data = {
    [UNCATEGORIZED]: []
  }
  wx.setStorageSync(NOTE_KEY, data)
  return data
}

// 获取所有笔记分类
function getCategories() {
  let notes = wx.getStorageSync(NOTE_KEY)
  if (!notes) {
    notes = initData()
  }
  return Object.keys(notes)
}

// 获取分类下的所有笔记
function getNotes() {
  let notes = wx.getStorageSync(NOTE_KEY)
  if (!notes) {
    notes = initData()
  }
  return notes
}

// 获取分类下的所有笔记
function getNotesByCategory(category) {
  let notes = wx.getStorageSync(NOTE_KEY)
  if (!notes) {
    notes = initData()
  }
  return notes[category] || []
}

// 获取单个笔记
function getNote(category, id) {
  const notes = getNotesByCategory(category)
  return notes.find(note => note.id === id)
}

// 保存笔记
function saveNote(category, note) {
  let notes = wx.getStorageSync(NOTE_KEY)
  if (!notes) {
    notes = initData()
  }

  // 如果没有传入分类，使用未分类
  const targetCategory = category || UNCATEGORIZED

  if (!notes[targetCategory]) {
    notes[targetCategory] = []
  }

  // 如果是新笔记
  if (!note.id) {
    note.id = 'note' + Date.now().toString()
    note.createdAt = new Date().toISOString()
    note.updatedAt = new Date().toISOString()
    notes[targetCategory].unshift(note) // 添加到开头
  } else {
    // 更新现有笔记
    note.updatedAt = new Date().toISOString()

    const index = notes[targetCategory].findIndex(n => n.id === note.id)
    if (index !== -1) {
      notes[targetCategory][index] = note
    }
  }

  console.log('storage.js -> saveNote - > notes: ', notes)

  wx.setStorageSync(NOTE_KEY, notes)
  return note
}

// 删除笔记
function deleteNote(category, id) {
  let notes = wx.getStorageSync(NOTE_KEY)
  if (!notes) return

  const targetCategory = category || UNCATEGORIZED
  if (notes[targetCategory]) {
    notes[targetCategory] = notes[targetCategory].filter(note => note.id !== id)
    wx.setStorageSync(NOTE_KEY, notes)
  }
}

// 删除笔记
function deleteNotesByCategory(category) {
  let notes = wx.getStorageSync(NOTE_KEY)
  if (!notes) return

  const targetCategory = category || UNCATEGORIZED
  if (notes[targetCategory]) {
    delete notes[targetCategory]
    // notes[targetCategory] = notes[targetCategory].filter(note => note.id !== id)
    wx.setStorageSync(NOTE_KEY, notes)
  }
}

// 添加分类
function addCategory(category) {
  if (!category || category === UNCATEGORIZED) return false

  let notes = wx.getStorageSync(NOTE_KEY)
  if (!notes) {
    notes = initData()
  }

  if (!notes[category]) {
    notes[category] = []
    wx.setStorageSync(NOTE_KEY, notes)
    return true
  }
  return false
}

// 删除分类
function deleteCategory(category) {
  if (!category || category === UNCATEGORIZED) return false

  let notes = wx.getStorageSync(NOTE_KEY)
  if (!notes) return false

  if (notes[category]) {
    // 将分类下的笔记移动到未分类
    if (notes[category].length > 0) {
      if (!notes[UNCATEGORIZED]) {
        notes[UNCATEGORIZED] = []
      }
      notes[UNCATEGORIZED].push(...notes[category].map(n => ({ ...n, category: UNCATEGORIZED })))
    }

    delete notes[category]
    wx.setStorageSync(NOTE_KEY, notes)
    return true
  }
  return false
}

// 移动笔记到其他分类
function moveNoteToCategory(noteId, fromCategory, toCategory) {
  let notes = wx.getStorageSync(NOTE_KEY)

  if (!notes) return false

  const sourceCategory = fromCategory || UNCATEGORIZED
  const targetCategory = toCategory || UNCATEGORIZED

  console.log(`sourceCategory:${sourceCategory},targetCategory:${targetCategory}`)

  if (!notes[sourceCategory] || !notes[targetCategory]) return false

  const noteIndex = notes[sourceCategory].findIndex(n => n.id === noteId)
  if (noteIndex === -1) return false

  const [note] = notes[sourceCategory].splice(noteIndex, 1)
  note.category = targetCategory
  notes[targetCategory].unshift(note)
  wx.setStorageSync(NOTE_KEY, notes)
  return true
}

module.exports = {
  UNCATEGORIZED,
  getCategories,
  getNotesByCategory,
  getNote,
  getNotes,
  saveNote,
  deleteNote,
  deleteNotesByCategory,
  addCategory,
  deleteCategory,
  moveNoteToCategory,
  getConfig,
  setConfig
}